package com.witframework.web.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.witframework.util.HtmlBuilder;

/**
 * 框架内置的检索表达式标签
 * <code>expression</code>检索表达式变量名称
 * <P><PRE>
 * 该标签支持的表达式(expression值)如下：
 * 等于表达式：<code>eq(propertyName,_fieldName)<code>
 * 大于等于表达式：ge(propertyName,_fieldName)
 * 大于表达式：gt(propertyName,_fieldName)
 * LIKE表达式：like(propertyName,_fieldName，matchMode)
 * 匹配模式：matchMode={R,L,A}:R为%value，L为value%,A为：%value%
 * 小于表达式：le(propertyName,_fieldName)
 * 小于等于表达式：lt(propertyName,_fieldName)
 * BETWEEN表达式：between(propertyName,_Lo_fieldName,_Hi_fieldName2)
 * propertyName为实体的属性名称；_fieldName为html的输入域名称，该输入域的值被用做检索条件值,其名称前缀必须为:_；
 * <code>_Lo_fieldName</code>:BETWEEN表达式的低值<oode>_Hi_fieldName</code>:BETWEEN表达式的高值
 * 
 * 检索表达式示例：
 * <code>
 * _username2:&lt;input name="_username2"/&gt;
 * &lt;ws:searchExpression expression="eq(username,_username2)"/&gt;
 * like(username,_username3)
 * _username3:&lt;input name="_username3" /&gt;
 * &lt;ws:searchExpression expression="like(username,_username3,R)"/&gt;
 * type:&lt;input name="_enabled" /&gt;
 * &lt;ws:searchExpression expression="like(enabled,_enabled,A)"/&gt; *
 * </code> 
 * </PRE><P>
 * @see com.witframework.ext.gdao.GenericHibernateDao#createCriteria(Class, java.util.Map)
 * @author wuxinyang
 *
 */
public class SearchExpressionTag extends TagSupport{
	//检索表达式变量名称
	private String expression;
	//styleClass
	private String styleClass=null;
	@Override
	public int doStartTag() throws JspException {		
		String name="_s_expression"+new java.util.Random().nextInt();		
		HtmlBuilder html=new HtmlBuilder();
		html.input().name(name);
		html.type("hidden");
		html.value(expression);
		if(getStyleClass()!=null){
			html.styleClass(styleClass);
		}
		html.close();	
		
		JspWriter writer = pageContext.getOut();
		try {
			writer.println(html.toString());
		} catch (IOException e) {
			throw new JspException("Print/Flush in LoadingTag tag error:" + e);
		}
		return (EVAL_BODY_INCLUDE);
	}
	public String getExpression() {
		return expression;
	}
	public void setExpression(String expression) {
		this.expression = expression;
	}
	public String getStyleClass() {
		return styleClass;
	}
	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}
}
